package com.cy.adapter;

/**
 * <br>@class: AdapterTest
 * <br>@description: 适配器模式
 * <br>@author: cy
 * <br>@date: 2020-03-28 14:44:58
 * <br>@version: 1.0
 * <br>
 * <br>@since: (版本) 作者 时间 注释
 */
public class AdapterTest {
    public static void main(String[] args) {
        //将一个类的接口转换成希望的另一个接口，Adapter模式使得原本由于接口不兼容而不能一起工作的类，变得兼容
        //对象模式，组合，引入原始对象，处理成目标对象
        PhonePower pp = new Adapter(new HomePower());
        pp.outputV5();
        //类模式，继承（缺点，暴露无关方法）
        AdapterEx adapterEx = new AdapterEx();
        adapterEx.outputV5();
    }
}

class HomePower {
    public int outputV220() {
        return 220;
    }
}

interface PhonePower {
    int outputV5();
}

class Adapter implements PhonePower {
    private HomePower hp;

    public Adapter(HomePower hp) {
        this.hp = hp;
    }

    @Override
    public int outputV5() {
        int i = hp.outputV220();
        //....
        System.out.println(String.format("输入 %d v--> 输出 %d v", i, 5));
        return 5;
    }
}

class AdapterEx extends HomePower implements PhonePower {

    @Override
    public int outputV5() {
        int i = outputV220();
        //....
        System.out.println(String.format("输入 %d v--> 输出 %d v", i, 5));
        return 5;
    }
}